package com.online.music.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.music.common.Result;
import com.online.music.entity.dto.PlaylistAddSongsDTO;
import com.online.music.entity.po.Song;
import com.online.music.utils.UserContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.online.music.service.IPlaylistService;
import com.online.music.entity.po.Playlist;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: playlist接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "playlist接口")
@RestController
@RequestMapping("/playlist")
public class PlaylistController {

    private final IPlaylistService playlistService;

    @Operation(summary = "查询所有Playlist")
    @GetMapping
    public Result<List<Playlist>> findAll() {
        return Result.success(playlistService.list());
    }

    @Operation(summary = "根据id查询Playlist")
    @GetMapping("/{id}")
    public Result<Playlist> findOne(@PathVariable Long id) {
        return Result.success(playlistService.getById(id));
    }

    @Operation(summary = "根据分页查询Playlist")
    @GetMapping("/page")
    public Result<Page<Playlist>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                           Playlist playlist) {
        return playlistService.findPage(pageNum, pageSize, playlist);
    }

    @Operation(summary = "添加或修改Playlist")
    @PostMapping
    public Result<Boolean> save(@RequestBody Playlist playlist) {
        playlist.setUserId(UserContextUtil.getUserId());
        return Result.success(playlistService.saveOrUpdate(playlist));
    }

    @Operation(summary = "根据id批量删除Playlist")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return playlistService.deleteBatch(ids);
    }

    @Operation(summary = "添加歌曲到歌单")
    @PostMapping("/add/songs")
    public Result<String> addSongs(@RequestBody PlaylistAddSongsDTO playlistDTO) {
        return playlistService.addSongsToPlayList(playlistDTO);
    }

    @Operation(summary = "在歌单中移除歌曲")
    @PostMapping("/remove/songs")
    public Result<String> removeSongs(@RequestBody PlaylistAddSongsDTO playlistDTO) {
        return playlistService.removeSongsFromPlayList(playlistDTO);
    }

    @Operation(summary = "获取Playlist的歌曲列表")
    @GetMapping("/get/songs/{id}")
    public Result<List<Song>> getPlaylistSongs(@PathVariable Long id, @RequestParam String name) {
        return playlistService.getPlaylistSongs(id, name);
    }

    @Operation(summary = "获取专辑可用歌曲")
    @GetMapping("/available/songs")
    public Result<List<Song>> getAvailableSongs(@RequestParam String name) {
        return playlistService.getAvailableSongs(name);
    }

}
